package com.dailyblue.java.mybatis.mybatis01.test;

import com.dailyblue.java.mybatis.mybatis01.bean.Emp;
import com.dailyblue.java.mybatis.mybatis01.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class App {

    private static Scanner scan = new Scanner(System.in);

    private static void a() throws IOException {
        // 加载配置config.xml文件
        InputStream in = Resources.getResourceAsStream("config.xml");
        // 建立会话工厂
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        // 建立Java和数据库之间的会话
        SqlSession session = ssf.openSession();
        // 获取到EmpMapper的对象（MyBatis产生的一个代理对象）
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.find();
        list.forEach(System.out::println);
        // 关闭
        session.close();
    }

    private static void b() throws IOException {
        Emp emp = new Emp();
        System.out.print("请输入姓名：");
        String ename = scan.next();
        System.out.print("请输入职位：");
        String job = scan.next();
        System.out.print("请输入上司ID：");
        int mgr = scan.nextInt();
        System.out.print("请输入工资：");
        double sal = scan.nextDouble();
        System.out.print("请输入奖金：");
        double comm = scan.nextDouble();
        System.out.print("请输入部门ID：");
        int deptNo = scan.nextInt();
        emp.setComm(comm);
        emp.setDeptNo(deptNo);
        emp.setEname(ename);
        emp.setJob(job);
        emp.setMgr(mgr);
        emp.setSal(sal);
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.save(emp);
        session.commit();
        session.close();
    }

    private static void c() throws IOException {
        Emp emp = new Emp();
        System.out.print("请输入姓名：");
        String ename = scan.next();
        System.out.print("请输入职位：");
        String job = scan.next();
        System.out.print("请输入上司ID：");
        int mgr = scan.nextInt();
        System.out.print("请输入工资：");
        double sal = scan.nextDouble();
        System.out.print("请输入奖金：");
        double comm = scan.nextDouble();
        System.out.print("请输入部门ID：");
        int deptNo = scan.nextInt();
        System.out.print("请输入员工ID");
        int empNo = scan.nextInt();
        emp.setComm(comm);
        emp.setDeptNo(deptNo);
        emp.setEname(ename);
        emp.setJob(job);
        emp.setMgr(mgr);
        emp.setSal(sal);
        emp.setEmpNo(empNo);
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.update(emp);
        session.commit();
        session.close();
    }

    private static void d() throws IOException {
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.delete(5);
        session.commit();
        session.close();
    }

    private static void e() throws IOException {
        Emp emp = new Emp();
        System.out.print("请输入姓名：");
        String ename = scan.next();
        System.out.print("请输入职位：");
        String job = scan.next();
        System.out.print("请输入上司ID：");
        int mgr = scan.nextInt();
        System.out.print("请输入工资：");
        double sal = scan.nextDouble();
        System.out.print("请输入奖金：");
        double comm = scan.nextDouble();
        System.out.print("请输入部门ID：");
        int deptNo = scan.nextInt();
        System.out.println("请输入要删除的员工ID：");
        int id = scan.nextInt();
        emp.setComm(comm);
        emp.setDeptNo(deptNo);
        emp.setEname(ename);
        emp.setJob(job);
        emp.setMgr(mgr);
        emp.setSal(sal);
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper empMapper = session.getMapper(EmpMapper.class);
        empMapper.delete(id); // 一定会成功
        empMapper.save(emp); // 失败
        session.commit();
        session.close();
    }

    public static void main(String[] args) throws IOException {
        e();
    }
}
